<?php

/**
 * Project: Vietsam
 * Package Controller
 * Class: Tab
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *  
 * */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('user_model', 'user', true);
        $this->load->model('profile_model', 'profile', true);
        $this->load->helper('form');
        $this->checkLogin();
        $this->checkPermission();

        $this->template->set_template('admin');
        $this->buildMenu();
        $this->template->add_js('js/jquery1.6.1.js');
    }

    function User() {
        parent::__construct();
        $this->load->library('xajax', 'profile');
        $this->xajax->registerFunction(array('test', &$this, 'test'));

        $this->xajax->processRequest();
        $this->xajax_js = $this->xajax->getJavascript($this->base_url . 'js/');
        $this->load->model('user_model', 'user', true);
        $this->load->model('profile_model', 'profile', true);
        $this->load->helper('form');
        $this->checkLogin();
        $this->checkPermission();
    }

    function index() {
        $this->view();
    }

    function create() {
        $data = array();
        $data['id'] = 0;
        $data['firstname'] = '';
        $data['lastname'] = '';
        $data['email'] = '';
        $data['active'] = 1;
        $profiles = $this->profile->getAll();

        $profile_option = array('' => lang('select'));
        for ($i = 0; $i < count($profiles); $i++) {
            $profile_option[$profiles[$i]['id_profile']] = $profiles[$i]['vi_name'];
        }
        $data['profile'] = $profile_option;
        $data['id_profile'] = '';
        $data['error_message'] = '';
        $data['legend_text'] = lang('create_user');
        $this->template->parse_view('content', 'admin/user/create_user', $data, true);
        $this->template->render();
    }

    function edit($id_user) {
        $user = $this->user->getById($id_user);
        if (!$user) {
            redirect('admin/user');
            return;
        }

        $data['id'] = $user->id_user;
        $data['firstname'] = $user->firstname;
        $data['lastname'] = $user->lastname;
        $data['email'] = $user->email;
        $data['active'] = $user->active;
        $data['id_profile'] = $user->id_profile;

        $profiles = $this->profile->getAll();

        $profile_option = array('' => lang('select'));
        for ($i = 0; $i < count($profiles); $i++) {
            $profile_option[$profiles[$i]['id_profile']] = $profiles[$i]['vi_name'];
        }
        
        $data['profile'] = $profile_option;

        $data['error_message'] = '';
        $data['legend_text'] = lang('edit_user');

        $this->template->parse_view('content', 'admin/user/create_user', $data, true);
        $this->template->render();
    }

    function save() {
        $id = (int) $this->input->post('txtID');
        $user = new stdClass();
        $_email = '';
        if ($id > 0) {
            $user = $this->user->getById($id);

            if (!$user) {
                redirect('admin/user');
                return;
            }

            $_email = $user->email;
            $user->active_email = '';
        } else {
            $user->id_user = 0;
        }

        $user->firstname = trim($this->input->post('txtFirstname'));
        $user->lastname = trim($this->input->post('txtLastname'));
        $user->email = trim($this->input->post('txtEmail'));

        if (trim($this->input->post('txtPassword')) != '')
            $user->password = md5(trim($this->input->post('txtPassword')));

        $user->active = (int) $this->input->post('active');
        $user->id_profile = (int) ($this->input->post('id_profile'));

        $error = false;
        $error_message = '';

        $this->load->library('form_validation');

        $this->form_validation->set_rules('txtFirstname', 'lang:firstname', 'required|min_length[2]|max_length[60]');
        $this->form_validation->set_rules('txtLastname', 'lang:lastname', 'required|min_length[2]|max_length[60]');

        if ($id == 0)
            $this->form_validation->set_rules('txtEmail', 'lang:email', 'required|valid_email|callback_email_check');
        elseif ($id > 0 AND $user->email != $_email) {
            $this->form_validation->set_rules('txtEmail', 'lang:email', 'required|valid_email|callback_email_check');
        }

        if ($id == 0)
            $this->form_validation->set_rules('txtPassword', 'lang:password', 'required|min_length[4]|max_length[12]');
        else
            $this->form_validation->set_rules('txtPassword', 'lang:password', 'min_length[4]|max_length[12]');

        //$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('id_profile', 'lang:profile', 'callback_profile_check');


        if ($this->form_validation->run() == FALSE) {
            $data['id'] = $user->id_user;
            $data['firstname'] = $user->firstname;
            $data['lastname'] = $user->lastname;
            $data['email'] = $user->email;
            $data['active'] = $user->active;

            $profiles = $this->profile->getAll();
            $profile_option = array('' => lang('select'));
            for ($i = 0; $i < count($profiles); $i++) {
                $profile_option[$profiles[$i]['id_profile']] = $profiles[$i]['vi_name'];
            }
            $data['profile'] = $profile_option;
            $data['id_profile'] = $user->id_profile;

            $data['error_message'] = '<div style="color:red">' . validation_errors() . '</div>';
            $data['legend_text'] = $id > 0 ? lang('edit_user') : lang('create_user');
            $this->template->parse_view('content', 'admin/user/create_user', $data, true);
            $this->template->render();
        } else {
            if ($id > 0) {
                $result = $this->user->update($user);
            } else {

                $this->user->firstname = $user->firstname;
                $this->user->lastname = $user->lastname;
                $this->user->email = $user->email;
                $this->user->active = $user->active;
                $this->user->password = $user->password;
                $this->user->active_email = '';
                $this->user->id_profile = $user->id_profile;
                //$this->user->password = $user->password;

                $id_user = $this->user->insert();

                if ($id_user > 0) {
                    //Get all tab:
                    /* $this->load->model('tab_model', 'tab', true);
                      $tabs = $this->tab->getAll(-1);
                      $id_tabs = array();
                      for($i = 0; $i < count($tabs); $i++){
                      $id_tabs[] = $tabs[$i]->id_tab;
                      }

                      $this->load->model('access_model', 'access', true);
                      $this->access->insert($id_profile, $id_tabs);
                     */
                }
            }

            redirect('admin/user/');
        }
    }

    function view() {
        $NUMBER_RECORD_PER_PAGE = 5;
        $this->load->library('pagination');
        //$parent = $this->uri->segment(4, 0);
        $page = (int) $this->uri->segment(4);
        $config['base_url'] = $this->base_url . 'admin/user/view';
        $config['total_rows'] = $this->user->getAllCount();
        $config['per_page'] = $NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 4;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);

        $rows = $this->user->getAll($page, $NUMBER_RECORD_PER_PAGE);
        $data = array();
        $data['rows'] = $rows;
        
        $this->template->parse_view('content', 'admin/user/user_list', $data, true);

        $this->template->render();
    }

    function delete() {
        $id = intval($this->uri->segment(4));
        $ids = array();
        if ($id > 0) {
            $ids[] = (int) $id;
            //$this->product->delete($id);
        } else {
            if (($this->input->post('submitDelcategory'))) {
                $ids = $this->input->post('categoryBox');
            }
        }

        if (in_array(1, $ids) OR in_array(2, $ids)) {
            redirect('admin/user/');
            return;
        }

        if (count($ids)) {

            $result = $this->user->delete($ids);
        }

        redirect('admin/user/');
    }

    function profile_check($value) {
        if ($value == 0) {
            $this->form_validation->set_message('profile_check', 'Chưa chọn profile');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function email_check($email) {
        $user = $this->user->getByEmail($email);
        if ($user) {
            $this->form_validation->set_message('email_check', lang('email_is_not_avaiable'));
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

?>
